package icom.acm.manager;
/**
 * @author shench
 */
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

import icom.acm.bean.T_USERACTIVATE;
import icom.acm.util.DBConnect;

public class T_UserActivateMgr {	
	
	public void insert(T_USERACTIVATE ua){
		String sql = "INSERT INTO T_USERACTIVATE(UA_U_ID, UA_KEY, UA_DATE) VALUES" +
				"(?, ?, NOW())";
		DBConnect db = new DBConnect();		
		db.prepareStatement(sql);
		db.setAutoCommit(false);
		db.setInt(1, ua.getUA_U_ID());
		db.setString(2, ua.getUA_KEY());		
		try {
			db.excute();
			db.commit();
		} catch (MySQLIntegrityConstraintViolationException e) {
			e.printStackTrace();
		} finally {
		db.setAutoCommit(true);
		db.close();}
	}

	public void insert0(T_USERACTIVATE ua, DBConnect db){
		String sql = "INSERT INTO T_USERACTIVATE(UA_U_ID, UA_KEY, UA_DATE) VALUES" +
				"(?, ?, NOW())";
		db.prepareStatement(sql);
		db.setAutoCommit(false);
		db.setInt(1, ua.getUA_U_ID());
		db.setString(2, ua.getUA_KEY());		
		try {
			db.excute();
			db.commit();
			db.setAutoCommit(true);
		} catch (MySQLIntegrityConstraintViolationException e) {
			e.printStackTrace();
			db.close();
		} finally {
		}
	}
	
	public T_USERACTIVATE selectByIdAndKeyAndDate0(T_USERACTIVATE ua, DBConnect db){
		T_USERACTIVATE tua = null;
		boolean flag = false;
		String sql = "SELECT UA_U_ID, UA_KEY, UA_DATE FROM T_USERACTIVATE " +
				"WHERE UA_U_ID="+ua.getUA_U_ID() + " AND UA_KEY='" + ua.getUA_KEY() +
				"' AND TIMESTAMPDIFF(HOUR, UA_DATE, NOW()) < 24";
System.out.println(sql);
		db.prepareStatement(sql);
		db.excuteQuery();
		try{
			tua = null;
			while (db.next()){								
				tua = new T_USERACTIVATE();
				tua.setUA_U_ID(db.getInt(1));
				tua.setUA_KEY(db.getString(2));
				tua.setUA_DATE(db.getDate(3));
			}
		}catch(Exception ex){flag=true;}finally{
			if (flag)
				db.close();
		}
		return tua;
	}

	public T_USERACTIVATE selectByIdAndKey0(T_USERACTIVATE ua, DBConnect db) {
		T_USERACTIVATE tua = null;
		boolean flag = false;
		String sql = "SELECT UA_U_ID, UA_KEY, UA_DATE FROM T_USERACTIVATE " +
				"WHERE UA_U_ID="+ua.getUA_U_ID() + " AND UA_KEY='" + ua.getUA_KEY() + "'";
System.out.println(sql);
		db.prepareStatement(sql);
		db.excuteQuery();
		try{
			while (db.next()){								
				tua = new T_USERACTIVATE();
				tua.setUA_U_ID(db.getInt(1));
				tua.setUA_KEY(db.getString(2));
				tua.setUA_DATE(db.getDate(3));
			}
		}catch(Exception ex){flag=true;}finally{
			if (flag)
				db.close();
		}
		return tua;
	}
	
	public void delete0(T_USERACTIVATE ua, DBConnect db){
		String sql = "DELETE FROM T_USERACTIVATE WHERE UA_U_ID="+ua.getUA_U_ID();
		db.prepareStatement(sql);
		db.excuteUpdate();
	}

	public T_USERACTIVATE selectById0(T_USERACTIVATE ua, DBConnect db) {
		T_USERACTIVATE tua = null;
		boolean flag = false;
		String sql = "SELECT UA_U_ID, UA_KEY, UA_DATE FROM T_USERACTIVATE " +
				"WHERE UA_U_ID="+ua.getUA_U_ID();
System.out.println(sql);
		db.prepareStatement(sql);
		db.excuteQuery();
		try{
			while (db.next()){								
				tua = new T_USERACTIVATE();
				tua.setUA_U_ID(db.getInt(1));
				tua.setUA_KEY(db.getString(2));
				tua.setUA_DATE(db.getDate(3));
			}
		}catch(Exception ex){flag=true;}finally{
			if (flag)
				db.close();
		}
		return tua;
	}
}
